package top.redis;

import lombok.Data;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import utils.ContextUtils;

import java.util.*;

@Data
public class RedisTest {


    public static void main(String[] args) {
        testTranslate();
    }

    public static void testTranslate() {
        ClassPathXmlApplicationContext context = ContextUtils.getContext();
        RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);

        SessionCallback callback1 = new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                // 1、开启事务
                redisOperations.multi();

                // 2、将需要执行的命令放入到队列中
                redisOperations.boundValueOps("key2").set("20191211");
                redisOperations.boundValueOps("key3").set("20191212");


                // 3、执行事务

                return null;
            }
        };

        SessionCallback callback = (SessionCallback) (RedisOperations op) -> {
            op.multi();
            // 2、将需要操作的命令放入到队列中
            op.boundValueOps("key1").set("value1");
            String value = (String) op.boundValueOps("key1").get();
            System.out.println("此时所有的redis操作都是写入到队列中还没有被执行，所以此处获取不到值，key1=" + value);
            List<Object> results = op.exec();
            System.out.println(results);
            value = (String) redisTemplate.opsForValue().get("key1");
            return value;
        };

        String value = (String) redisTemplate.execute(callback);
        System.out.println(value);
    }

    public static void testList(){
        ClassPathXmlApplicationContext context = ContextUtils.getContext();
        RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);
        try {
            // 删除链表，以便我们可以反复测试
            redisTemplate.delete("list");
            redisTemplate.opsForList().leftPush("list", "node3");
            List<String> nodeList = new ArrayList<String>();
            for (int i = 2; i >= 1; i--) {
                nodeList.add("node" + i);
            }
            redisTemplate.opsForList().leftPushAll("list", nodeList);
            redisTemplate.opsForList().rightPush("list", "node4");
            String node1 = (String) redisTemplate.opsForList().index("list", 0);
            long size = redisTemplate.opsForList().size("list");
            String lpop = (String) redisTemplate.opsForList().leftPop("list");
            String rpop = (String) redisTemplate.opsForList().rightPop("list");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void testRedisHash(){
        ClassPathXmlApplicationContext context = ContextUtils.getContext();
        RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);
        String key = "hash";
        Map<String, String> map = new HashMap<>();
        map.put("f1", "val1");
        map.put("f2", "val2");
        redisTemplate.opsForHash().putAll(key,map);
        redisTemplate.opsForHash().put(key, "f3", "6");
        printValueForhash(redisTemplate,key,"f2");
        boolean exists = redisTemplate.opsForHash().hasKey(key, "f3");
        System.out.println(exists);
        Map keyValMap = redisTemplate.opsForHash().entries(key);
        redisTemplate.opsForHash().increment(key, "f3", 2);
        printValueForhash(redisTemplate, key, "f3");
        List valueList = redisTemplate.opsForHash().values(key);
        Set keyList = redisTemplate.opsForHash().keys(key);
        List valueList2 = redisTemplate.opsForHash().multiGet(key, keyList);
        boolean success = redisTemplate.opsForHash().putIfAbsent(key, "f4", "val4");
        System.out.println(success);
        redisTemplate.opsForHash().delete(key,"f1","f2");
        Map keyValMap2 = redisTemplate.opsForHash().entries(key);
    }

    public static void printValueForhash(RedisTemplate redisTemplate,String key,String field){
        Object value = redisTemplate.opsForHash().get(key, field);
        System.out.println(value);
    }

    public static void testCal(){
        ClassPathXmlApplicationContext context = ContextUtils.getContext();
        RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);
        redisTemplate.opsForValue().set("i", "9");
        printCurrValue(redisTemplate, "i");
        redisTemplate.opsForValue().increment("i", 1);
        printCurrValue(redisTemplate, "i");
        redisTemplate.getConnectionFactory().getConnection().decr(redisTemplate.getKeySerializer().serialize("i"));
        printCurrValue(redisTemplate,"i");
        redisTemplate.opsForValue().increment("i", 2.3);
        printCurrValue(redisTemplate,"i");
    }

    public static void printCurrValue(RedisTemplate redisTemplate, String key) {
        String i = (String) redisTemplate.opsForValue().get(key);
        System.err.println(i);
    }



}
